{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ce4d7d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试用例_CTypes_C_Model-String_Like_List_Jupyter_Nb_GF_2024-09-30.ipynb\n",
    "# Environment: Python 3.8.0\n",
    "# Create by GF 2024-09-30 16:46"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "241794f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compile by GCC:\n",
    "# --------------------------------------------------\n",
    "# gcc -fPIC -o lib.statistics.o -c GF_C_MODEL_STRING_LIKE_LIST_GB2312.c && gcc -shared -o lib.statistics.so lib.statistics.o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4dc3e384",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ctypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a56036f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "CLIB = ctypes.cdll.LoadLibrary(\"./lib.statistics.so\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e7442588",
   "metadata": {},
   "outputs": [],
   "source": [
    "CLIB.STRING_LIKE_LIST_APPEND_ELEM.argtypes = [ctypes.c_char_p, ctypes.c_char_p]\n",
    "CLIB.STRING_LIKE_LIST_APPEND_ELEM.restype  = ctypes.c_char_p\n",
    "# --------------------------------------------------\n",
    "CLIB.STRING_LIKE_LIST_EXTRACT_ELEMENT_BY_INDEX.argtypes = [ctypes.c_char_p, ctypes.c_int]\n",
    "CLIB.STRING_LIKE_LIST_EXTRACT_ELEMENT_BY_INDEX.restype  = ctypes.c_char_p\n",
    "# --------------------------------------------------\n",
    "CLIB.STRING_LIKE_LIST_REPLACE_VALUE_BY_INDEX.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p]\n",
    "CLIB.STRING_LIKE_LIST_REPLACE_VALUE_BY_INDEX.restype  = ctypes.c_char_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0cfdf69c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def PY3_STR_TO_C_CHAR_P(INPUT:str) -> str:\n",
    "\n",
    "    PY3_BYTES = INPUT.encode(\"utf-8\")\n",
    "    C_CHAR_P  = ctypes.create_string_buffer(PY3_BYTES)\n",
    "    return C_CHAR_P\n",
    "\n",
    "def C_CHAR_P_TO_PY3_STR(INPUT:str) -> object:\n",
    "\n",
    "    if INPUT:\n",
    "        PY3_STRING = INPUT.decode(\"utf-8\")\n",
    "        # CLIB.free(INPUT) # -> Release \"Char Pointer\" Memory Allocated by C Function Call.\n",
    "        return PY3_STRING\n",
    "    else:\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8c326f5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exception: [A, B, C] => [A, B, C, D] | Result: [[A, B, C, D]\n"
     ]
    }
   ],
   "source": [
    "# String Like List: Append Element.\n",
    "# --------------------------------------------------\n",
    "ARGS   = [PY3_STR_TO_C_CHAR_P(\"[A, B, C]\"), PY3_STR_TO_C_CHAR_P(\"D\")]\n",
    "Result = CLIB.STRING_LIKE_LIST_APPEND_ELEM(ARGS[0], ARGS[1])\n",
    "Result = C_CHAR_P_TO_PY3_STR(Result)\n",
    "print(\"Exception: [A, B, C] => [A, B, C, D] | Result: %s\" % Result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e710f38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exception: [A, B, C] => B | Result: B\n"
     ]
    }
   ],
   "source": [
    "# String Like List: Extract Element by Index.\n",
    "# --------------------------------------------------\n",
    "ARGS   = [PY3_STR_TO_C_CHAR_P(\"[A, B, C]\"), 1]\n",
    "Result = CLIB.STRING_LIKE_LIST_EXTRACT_ELEMENT_BY_INDEX(ARGS[0], ARGS[1])\n",
    "Result = C_CHAR_P_TO_PY3_STR(Result)\n",
    "print(\"Exception: [A, B, C] => B | Result: %s\" % Result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a90a2735",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exception: [A, B, C] => [1, B, C] | Result: [1, B, C]\n",
      "Exception: [A, B, C] => [A, 2, C] | Result: [A, 2, C]\n",
      "Exception: [A, B, C] => [A, B, 3] | Result: [A, B, 3]\n"
     ]
    }
   ],
   "source": [
    "# String Like List: Replace Value by Index.\n",
    "# --------------------------------------------------\n",
    "ARGS   = [PY3_STR_TO_C_CHAR_P(\"[A, B, C]\"), 0, PY3_STR_TO_C_CHAR_P(\"1\")]\n",
    "Result = CLIB.STRING_LIKE_LIST_REPLACE_VALUE_BY_INDEX(ARGS[0], ARGS[1], ARGS[2])\n",
    "Result = C_CHAR_P_TO_PY3_STR(Result)\n",
    "print(\"Exception: [A, B, C] => [1, B, C] | Result: %s\" % Result)\n",
    "# --------------------------------------------------\n",
    "ARGS   = [PY3_STR_TO_C_CHAR_P(\"[A, B, C]\"), 1, PY3_STR_TO_C_CHAR_P(\"2\")]\n",
    "Result = CLIB.STRING_LIKE_LIST_REPLACE_VALUE_BY_INDEX(ARGS[0], ARGS[1], ARGS[2])\n",
    "Result = C_CHAR_P_TO_PY3_STR(Result)\n",
    "print(\"Exception: [A, B, C] => [A, 2, C] | Result: %s\" % Result)\n",
    "# --------------------------------------------------\n",
    "ARGS   = [PY3_STR_TO_C_CHAR_P(\"[A, B, C]\"), 2, PY3_STR_TO_C_CHAR_P(\"3\")]\n",
    "Result = CLIB.STRING_LIKE_LIST_REPLACE_VALUE_BY_INDEX(ARGS[0], ARGS[1], ARGS[2])\n",
    "Result = C_CHAR_P_TO_PY3_STR(Result)\n",
    "print(\"Exception: [A, B, C] => [A, B, 3] | Result: %s\" % Result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad624f9f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
